<?php
/**
 * Created by PhpStorm.
 * User: zouhua
 * Date: 2017/6/8
 * Time: 15:17
 */

namespace app\helpers;


use think\Image;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;

class AppHelper {

    const ERROR_CODE = 1;
    const SUCCESS_CODE = 0;

    const QI_NIU_DOMAIN = 'http://p6yhjn52d.bkt.clouddn.com/';

    public static function generateUUid($n = 12, $uid = '') {
        if (NULL === $uid) {
            $uid = '';
        }
        $uuid = uniqid($uid);
        $rand = mt_rand(10000000, 99999999);
        $str = substr(md5($uuid . $rand), 8, 8);
        return substr(bin2hex($str), 0, $n);
    }

    public static function success($data = NULL, $msg = '') {
        return ['msg' => $msg, 'code' => self::SUCCESS_CODE, 'data' => $data];
    }

    public static function isOk($res) {
        return $res['code'] === self::SUCCESS_CODE;
    }

    public static function error($msg = NULL, $code = self::ERROR_CODE, $data = NULL) {
        if (NULL === $msg) {
            $msg = '网络异常，请稍后再试！';
        }
        return ['msg' => $msg, 'code' => $code, 'data' => $data];
    }

    public static function getClientIp($encode = FALSE) {
        $ip = \Yii::$app->request->getUserIP();
        if (!$ip) {
            $ip = '0.0.0.0';
        }
        return $encode ? static::encodeIp($ip) : $ip;
    }

    public static function encodeIp($ip) {
        return sprintf("%u", ip2long($ip));
    }

    public static function decodeIp($ip) {
        return long2ip($ip);
    }

    public static function hidePhone($phone) {
        return substr_replace($phone, '****', 3, 4);
    }

    public static function qiNiuImage($key) {
        return self::QI_NIU_DOMAIN . $key;
    }

    public static function formatImage($avatar) {
        if (empty($avatar)) {
            return '';
        }

        if (strpos($avatar, 'http') !== FALSE) {
            return $avatar;
        }

        if (strpos($avatar, 'uploads') !== FALSE) {
            $avatar = '/' . trim($avatar, '/');
            if (strpos($avatar, 'http') === FALSE) {
                $host = str_replace('www', 'f1', \Yii::$app->request->hostInfo);
                $avatar = $host . $avatar;
            }
            return $avatar;
        }
        return $avatar;
    }

    public static function formatThumb($file) {
        $info = pathinfo($file);
        $filename = $info['filename'];
        $thumbFile = str_replace($filename, "{$filename}_thumb", $file);
        if (file_exists(APP_ROOT . $thumbFile)) {
            return static::formatImage($thumbFile);
        }

        $image = Image::open(APP_ROOT . $file);
        $image->thumb(400, 400)->save(APP_ROOT . $thumbFile, NULL, 95);
        return static::formatImage($thumbFile);
    }

    public static function formatImages($data) {
        if (empty($data)) {
            return [];
        }
        $items = [];
        foreach ($data as $img) {
            $items[] = self::formatImage($img);
        }
        return $items;
    }

    public static function isValid($phone) {
        return preg_match('/^(1\d{10})$/', $phone);
    }

    public static function img($src, $options = ['style' => 'width:300px;']) {
        if (empty($src)) {
            return '';
        }
        return Html::img(self::formatImage($src), $options);
    }

    public static function toImages($data, $options = []) {
        $data = Json::decode($data, TRUE);
        if (empty($data)) {
            return '';
        }
        $items = [];
        foreach ($data as $img) {
            $items[] = self::toImage($img, $options);
        }
        return join("\n", $items);
    }

    public static function toImage($img, $options = []) {
        $src = self::img($img, $options);
        return Html::a($src, self::formatImage($img), ['target' => '_blank']);
    }

    public static function formatTag($tag, $type = NULL) {
        $tagList = explode(',', $tag);

        $data = [];
        foreach ($tagList as $item) {
            $item = trim($item);
            if ($item) {
                $data[] = Html::a($item, Url::to(['search/', 'id' => $item]), ['class' => 'tag-item']);
            }
        }
        return join("\n", $data);
    }

    public static function getPlayName($i) {
        $names = [0 => '一', 1 => '二', 2 => '三'];
        return $names[$i];
    }
}